//
//  LoginViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/13.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "ForgetPwdViewController.h"
#import "LineDownViewController.h"
#import "LineOnViewController.h"
#import "RankViewController.h"
#import "TabbarViewController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIView *FirstView;
@property (weak, nonatomic) IBOutlet UIView *SecondView;
@property (weak, nonatomic) IBOutlet UILabel *TitleLabel;
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *PasswordTF;
@property (weak, nonatomic) IBOutlet UIButton *LoginBtn;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _PhoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _TitleLabel.layer.borderColor = [[UIColor whiteColor]CGColor];
    _TitleLabel.layer.borderWidth = 1.0f;
    [_TitleLabel.layer setCornerRadius:15.0];
    _TitleLabel.layer.masksToBounds = YES;
    
    _FirstView.layer.borderColor = [[UIColor colorWithHex:0xebebeb]CGColor];
    _FirstView.layer.borderWidth = 1.0f;
    [_FirstView.layer setCornerRadius:17.0];
    _FirstView.layer.masksToBounds = YES;
    
    _SecondView.layer.borderColor = [[UIColor colorWithHex:0xebebeb]CGColor];
    _SecondView.layer.borderWidth = 1.0f;
    [_SecondView.layer setCornerRadius:17.0];
    _SecondView.layer.masksToBounds = YES;
    
    [_LoginBtn.layer setCornerRadius:20.0];
    _LoginBtn.layer.masksToBounds = YES;
    
    self.fd_interactivePopDisabled = YES;

}

- (IBAction)RegistBtnClick:(UIButton *)sender {
    RegistViewController *regist = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:regist animated:YES];
}

- (IBAction)ForgetBtnClick:(UIButton *)sender {
    ForgetPwdViewController *forget = [[ForgetPwdViewController alloc] init];
    [self.navigationController pushViewController:forget animated:YES];
}

- (IBAction)LoginClick:(UIButton *)sender {
    NSUUID *macUDID = [UIDevice currentDevice].identifierForVendor;
    NSString *test = [NSString stringWithFormat:@"%@", macUDID];
    if (_PhoneTF.text.length != 11) {
        [self.view Message:@"请输入正确的手机号" HiddenAfterDelay:1];
        return;
    }
    if (_PasswordTF.text.length == 0) {
        [self.view Message:@"请输入密码" HiddenAfterDelay:1];
        return;
    }
    [self.view loadingOnAnyView];
    NSData *PSD = [_PasswordTF.text
                   dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64PSD = [PSD base64EncodedStringWithOptions:0];
    NSDictionary *dic = @{@"loginName":_PhoneTF.text,@"loginPwd":base64PSD,@"loginIp":test};
    [THWManager GetHttp:API(Login) Parameters:dic Success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [self.view removeAnyView];
            if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
                //登录成功把userToken保存在本地
                [Utils setUserToken:String(responseObject[@"data"])];
//                TabbarViewController *tabbar = [[TabbarViewController alloc] init];
//                self.view.window.rootViewController = tabbar;
                [self.view Message:responseObject[@"msg"]];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccess" object:nil];
                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            }else{
                [self.view Message:responseObject[@"msg"]];
            }
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_PhoneTF resignFirstResponder];
    [_PasswordTF resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
